//
//  SEGIntegralExchangeTableViewCell.m
//  Commom
//
//  Created by Clavis on 2019/12/9.
//

#import "SEGIntegralExchangeTableViewCell.h"
#import <ReactiveObjC/ReactiveObjC.h>

#define SEGExchangeCellHeight 53.0
#define SEGExchangeCellWidth LYScreenWidth()-30

@interface SEGIntegralExchangeTableViewCell ()

@property (nonatomic, strong) UIView *exchangeView;
@property (nonatomic, strong) UIView *fistPageView;
@property (nonatomic, strong) UIView *secondPageView;
//@property (nonatomic, strong) NSMutableArray *fistPageSubViewArray;
//@property (nonatomic, strong) NSMutableArray *secondPageSubViewArray;
@property (nonatomic, strong) SEGIntegralExchangeSubCell *firstExchangeCell;
@property (nonatomic, strong) SEGIntegralExchangeSubCell *secondExchangeCell;
@property (nonatomic, strong) NSArray<SEGIntegralExchangeModel*> *exchangeModelArray;
@property (nonatomic, strong) dispatch_source_t timer;
@property (nonatomic, assign) __block NSInteger currentPage;

/// 定时器取消
@property (nonatomic, strong) RACDisposable *disposable;

@end

@implementation SEGIntegralExchangeTableViewCell

- (void)dealloc {
//    [self.timer invalidate];
    NSLog(@"%s", __func__);
    [self.disposable dispose];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = kColor_W;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = kColor_W;
    [self.contentView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    UILabel *titleLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [titleView addSubview:label];
        label.textColor = kColor_1;
        label.font = [UIFont boldSystemFontOfSize:15];
        label.text = @"积分抢兑";
        label;
    });
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_offset(0);
    }];
    
    UIImageView *arrow = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [titleView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = SEGAutoPicName(@"btn_more_gray_activity_integrate");
        imageView;
    });
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(6, 11));
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColor_L2;
    [titleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    self.exchangeView = [[UIView alloc] init];
    self.exchangeView.backgroundColor = kColor_W;
    self.exchangeView.clipsToBounds = YES;
    [self.contentView addSubview:self.exchangeView];
    [self.exchangeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.equalTo(titleView.mas_bottom);
        make.height.mas_equalTo(SEGExchangeCellHeight);
    }];
    
    self.fistPageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SEGExchangeCellWidth, SEGExchangeCellHeight)];
    [self.exchangeView addSubview:self.fistPageView];
    self.firstExchangeCell = [[SEGIntegralExchangeSubCell alloc] init];
    self.firstExchangeCell.frame = CGRectMake(0, 15, SEGExchangeCellWidth, 23);
    [self.fistPageView addSubview:self.firstExchangeCell];
    
    self.secondPageView = [[UIView alloc] initWithFrame:CGRectMake(0, SEGExchangeCellHeight, SEGExchangeCellWidth, SEGExchangeCellHeight)];
    [self.exchangeView addSubview:self.secondPageView];
    self.secondExchangeCell = [[SEGIntegralExchangeSubCell alloc] init];
    self.secondExchangeCell.frame = CGRectMake(0, 15, SEGExchangeCellWidth, 23);
    [self.secondPageView addSubview:self.secondExchangeCell];
    
    UIButton *jumpButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(jumpButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        button;
    });
    [jumpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = kColor_bg;
    [self.contentView addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(5);
    }];
}

- (void)jumpButtonTap:(UIButton *)sender {
    NSDictionary *params = @{@"linkURL":kIntegarlShop};
    UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initWebViewLoadController:params];
    [self.viewController.navigationController pushViewController:VC animated:YES];
}

- (void)updateCellWithModelArray:(NSArray<SEGIntegralExchangeModel *> *)modelArray {
    // 只赋值一次
    if (!self.exchangeModelArray) {
        self.exchangeModelArray = modelArray;
        [self updateExchangeCell:self.firstExchangeCell displayIndex:0];
        [self updateExchangeCell:self.secondExchangeCell displayIndex:1];

        // 积分记录多于一条才开始轮播
        if (self.exchangeModelArray.count > 1) {
            if (!self.disposable) {
                @weakify(self);
                self.disposable = [[RACSignal interval:3 onScheduler:[RACScheduler mainThreadScheduler] withLeeway:3] subscribeNext:^(NSDate * _Nullable x) {
                    @strongify(self);
                    [UIView animateWithDuration:1 animations:^{
                        self.fistPageView.y -= SEGExchangeCellHeight;
                        self.secondPageView.y -= SEGExchangeCellHeight;
                    } completion:^(BOOL finished) {
                        self.currentPage ++;
                        CGRect firstRect = CGRectMake(0, 0, SEGExchangeCellWidth, SEGExchangeCellHeight);
                        CGRect secondRect = CGRectMake(0, SEGExchangeCellHeight, SEGExchangeCellWidth, SEGExchangeCellHeight);
                        if (self.currentPage%2) {
                            self.secondPageView.frame = firstRect;
                            self.fistPageView.frame = secondRect;
                            [self updateExchangeCell:self.firstExchangeCell displayIndex:self.currentPage+1];
                        } else {
                            self.fistPageView.frame = firstRect;
                            self.secondPageView.frame = secondRect;
                            [self updateExchangeCell:self.secondExchangeCell displayIndex:self.currentPage+1];
                        }
                    }];
                }];
            }
        }
    }
}

- (void)updateExchangeCell:(SEGIntegralExchangeSubCell*)exchangeCell displayIndex:(NSInteger)index {
    NSInteger displayIndex = (index)%self.exchangeModelArray.count;
    SEGIntegralExchangeModel *exchangeModel = nil;
    if (displayIndex < self.exchangeModelArray.count) {
        exchangeModel = self.exchangeModelArray[displayIndex];
    }
    [exchangeCell updateCellWithModel:exchangeModel];
}

- (void)endTimer {
    [self.disposable dispose];
}

@end


@interface SEGIntegralExchangeSubCell ()

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *name;

@end

@implementation SEGIntegralExchangeSubCell

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.icon = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")];
        imageView.layer.cornerRadius = 5;
        imageView.clipsToBounds = YES;
        imageView;
    });
    self.icon.frame = CGRectMake(0, 0, 23, 23);
    
    self.name = ({
        UILabel *label = [[UILabel alloc] init];
        [self addSubview:label];
        label.textColor = kColor_1;
        label.font = kFont_13;
        label.text = @"";
        label;
    });
    self.name.frame = CGRectMake(33, 0, LYScreenWidth() - 63, 23);
}

- (void)updateCellWithModel:(SEGIntegralExchangeModel *)model {
    self.icon.image = nil;
    self.name.text = nil;
    if (model) {
        [self.icon setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,model.icon]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")]];
        self.name.text = model.alert;
    }
}

@end
